#pragma once

#include "viml/optimization_pass.h"
#include "viml/simple_optimizer.h"
#include <memory>
#include <chrono>

namespace viml {

/**
 * 常量折叠Pass
 * 
 * 识别并计算常量表达式,用结果字面量替换
 */
class ConstantFoldingPass : public OptimizationPass {
public:
    ConstantFoldingPass() {
        statistics_.pass_name = "ConstantFolding";
    }
    
    bool run(Program* program) override;
    
    std::string get_name() const override {
        return "ConstantFolding";
    }
    
    std::string get_description() const override {
        return "Fold constant expressions at compile time";
    }
    
private:
    ConstantFolder folder_;
};

/**
 * 死代码消除Pass
 * 
 * 识别并移除不可达的代码
 */
class DeadCodeEliminationPass : public OptimizationPass {
public:
    DeadCodeEliminationPass() {
        statistics_.pass_name = "DeadCodeElimination";
    }
    
    bool run(Program* program) override;
    
    std::string get_name() const override {
        return "DeadCodeElimination";
    }
    
    std::string get_description() const override {
        return "Eliminate unreachable code paths";
    }
    
private:
    DeadCodeEliminator eliminator_;
};

/**
 * 寄存器分配Pass
 * 
 * 执行活性分析和寄存器重分配以减少寄存器使用
 */
class RegisterAllocationPass : public BytecodePass {
public:
    RegisterAllocationPass() {
        statistics_.pass_name = "RegisterAllocation";
    }
    
    bool run(Bytecode* bytecode) override;
    
    std::string get_name() const override {
        return "RegisterAllocation";
    }
    
    std::string get_description() const override {
        return "Optimize register allocation through liveness analysis";
    }
};

} // namespace viml
